home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / Book Chapters / 11 - Porting / Sample Code / FindFile.c < prev    next >
Text File  |  1995-03-28  |  2KB  |  78 lines

  1. /************************************************************************************
  2.  *                                                                                                                                                                    *
  3.  *    FindFile.c                                                                                                                                            *
  4.  *                                                                                                                                                                    *
  5.  *    ©1995 Douglas Grounds. All Rights Reserved.                                                                            *
  6.  *                                                                                                                                                                    *
  7.  *    This file contains a function to find a file based on vRefNum, dirId and name,    *
  8.  *    or by name alone (full or relative path).                                                                                *
  9.  *                                                                                                                                                                    *
  10.  ************************************************************************************/
  11.  
  12.  #include <Dialogs.h>
  13.  #include <Files.h>
  14.  #include <StandardFile.h>
  15.  #include <stdio.h>
  16.  #include <string.h>
  17.  #include <Strings.h>
  18.  #include <Types.h>
  19.  
  20.  #ifndef TRUE
  21.  #define TRUE        1
  22.  #endif
  23.  
  24.  #ifndef FALSE
  25.  #define FALSE    0
  26.  #endif
  27.  
  28.  #include "FindFile.h"
  29.  
  30.  #define kFindFileAlertId        128
  31.  
  32. /******************************************************
  33.  *    findFile.                                                                                    *
  34.  *                                                                                                        *
  35.  *    Finds a file by itself, or with some assistance.    *
  36.  *    To find a file with full path name, send in 0 for    *
  37.  *    vRefNum and dirId.                                                                *
  38.  ******************************************************/
  39.  
  40. Boolean findFile (short vRefNum, long dirId, char *fileName, FSSpec *foundFile)
  41. {
  42.     FSSpec                            searchFile;
  43.     Str255                            pascalVersion;
  44.     SFTypeList                    myTypes;
  45.     StandardFileReply        reply;
  46.     OSErr                                err;
  47.     
  48.     strcpy((char *)pascalVersion, fileName);
  49.     c2pstr((char *)pascalVersion);                        // Make look different on different compilers.
  50.     
  51.     err = FSMakeFSSpec(vRefNum, dirId, pascalVersion, &searchFile);
  52.     
  53.     if (err != noErr)
  54.     {
  55.         sprintf((char *)pascalVersion, "The file named “%s” could not be found. Would you like to look for it?",
  56.             foundFile);
  57.         c2pstr((char *)pascalVersion);
  58.         
  59.         InitCursor();
  60.         ParamText(pascalVersion, "\p", "\p", "\p");
  61.         
  62.         if (Alert(kFindFileAlertId, NULL) == 1)
  63.         {
  64.             StandardGetFile(NULL, 1, myTypes, &reply);
  65.             
  66.             if (reply.sfGood == FALSE)
  67.                 return FALSE;
  68.             else
  69.             {
  70.                 *foundFile = reply.sfFile;
  71.                 return TRUE;
  72.             }
  73.         }
  74.     }
  75.     
  76.     *foundFile = searchFile;
  77.     return TRUE;
  78. }